<script setup>
	import FirstTitle from "@comp/FirstTitle";
	import {
		Editor,
		Toolbar
	} from "@wangeditor/editor-for-vue";
	import {
		onMounted
	} from "vue";
	import {
		searchDataZhbz
	} from "@/api/search";
	import {
		addOrUpdateZhbz
	} from "@/api/addOrUpdate";


	const city = window.globalObj.name;
	const editorRef = shallowRef();
	const editorRef1 = shallowRef();

	const toolbarConfig = {};
	const toolbarConfig1 = {};
	const editorConfig = {
		placeholder: "请输入内容..."
	};
	const mode = ref("default");
	const mode1 = ref("default");

	// 加载html
	const initHtml = () => {
		searchDataZhbz({
			m: "原则要求"
		}).then((res) => {
			const {
				data
			} = res
			valueHtml.value = data.find((x) => x.k === "主要原则")?.v || "";
			valueHtml1.value = data.find((x) => x.k === "措施要求")?.v || "";
		})
	};

	const handleCreated = (editor) => {
		editorRef.value = editor; // 记录 editor 实例，重要！
	};
	const handleCreated1 = (editor) => {
		editorRef1.value = editor; // 记录 editor 实例，重要！
	};

	const valueHtml = ref(
		"制定物资、器材、经费保障方案和计划，组织物资、器材、经费的调度与储备，组织战时物资、器材、经费供应和保障。"
	);

	const valueHtml1 = ref(
		"①能源类重点保障煤、电、汽柴油、天然气等；②建材类重点保障钢材、木材、水泥、沙石、线缆等；③机电、机械装备类重点保障机械、汽车、机电等；④生活物资重点保障粮、油、肉类、蔬菜、水等生活基本保障用品。⑤早期和临战疏散期间按任务和标准提前配发，空袭阶段组织急需品临时供应；⑥对伤病人员、老人、婴幼儿所需物资，予以优先保障。"
	)

	const saveProgramme = () => {
		ElMessageBox.confirm(`确定要保存吗?`, "警告", {
			confirmButtonText: "确定",
			cancelButtonText: "取消",
			type: "warning",
			customClass: "message-box",
		}).then(() => {
			addOrUpdateZhbz({
					m: "原则要求",
					data: [{
							k: "主要原则",
							v: valueHtml.value,
						},
						{
							k: "措施要求",
							v: valueHtml1.value,
						}
					],
				})
				.then((result) => {
					if (result.code === 200) {
						ElMessage.success("操作成功");
						initHtml();
					} else {
						ElMessage.error(result.msg);
					}
				})
				.catch(() => {});
		});
	}

	onMounted(() => {
		initHtml();

	});
	onBeforeUnmount(() => {
		const editor = editorRef.value;
		const editor1 = editorRef1.value;
		if (!editor) return;
		if (!editor1) return;
		editor.destroy();
		editor1.destroy();
	});
</script>
<template>
	<div class="">
		<div class="top-15">
			主要明确综合保障的原则和具体要求等内容。
		</div>
		<FirstTitle name="主要原则" class="top-15" />
		<div style="border: 1px solid #ccc" class="top-15">
			<Toolbar style="border-bottom: 1px solid #ccc" :editor="editorRef" :defaultConfig="toolbarConfig"
				:mode="mode" />
			<Editor style="height: 500px; overflow-y: hidden" v-model="valueHtml" :defaultConfig="editorConfig"
				:mode="mode" @onCreated="handleCreated" />
		</div>

		<FirstTitle name="措施要求" class="top-15" />
		<div style="border: 1px solid #ccc" class="top-15">
			<Toolbar style="border-bottom: 1px solid #ccc" :editor="editorRef1" :defaultConfig="toolbarConfig1"
				:mode="mode1" />
			<Editor style="height: 500px; overflow-y: hidden" v-model="valueHtml1" :defaultConfig="editorConfig"
				:mode="mode1" @onCreated="handleCreated1" />
		</div>



		<div class="flex-justify-end top-20">
			<el-button type="primary" class="save-btn" @click="saveProgramme">保存</el-button>
		</div>
	</div>
</template>
<style scoped lang="scss"></style>